package com.xpec.c4.db.usertype;

import java.util.ArrayList;
import java.util.List;

import com.ms.db.usertype.BasicToString;
import com.xpec.c4.db.util.CodecTool;

/**
 * 把ArrayList转换为String<br>
 * 使用指定的分隔符，默认为";"
 * 
 * @author Bany
 * 
 */
public class ArrayListIntToString extends BasicToString {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4824318311261560317L;

	@Override
	public int[] sqlTypes() {
		return CLOB_TYPES;
	}

	@Override
	public Class<?> returnedClass() {
		return List.class;
	}

	@Override
	protected String assemble(Object obj) {
		List<?> arrayList = (List<?>) obj;
		StringBuffer strBuf = new StringBuffer();
		for (int i = 0; i < arrayList.size() - 1; i++) {
			CodecTool
					.append(strBuf, String.valueOf(arrayList.get(i)), SPLITTER);
		}
		if (arrayList.size() > 0) {
			strBuf.append(arrayList.get(arrayList.size() - 1));
		}
		return strBuf.toString();
	}

	@Override
	protected Object parse(String value) {
		if (value == null) {
			return null;
		}
		List<Integer> list = new ArrayList<Integer>();
		if (value == null || value.length() <= 0) {
			return list;
		}
		String[] strs = CodecTool.split(value, SPLITTER);
		for (int i = 0; i < strs.length; i++) {
			if (strs[i] != null) {
				list.add(Integer.valueOf(strs[i]));
			} else {
				list.add(null);
			}
		}
		return list;
	}

}
